extends Area2D
class_name AreaObj

var x:float = 200;
var y:float = 200;
var xv:float = 15
var yv:float = 15;
var isMove = false;

func _init():
	# 创建碰撞体
	self.position = Vector2(x,y)


	# 碰撞体外观
	var spr = Sprite.new();
	spr.texture = load("res://icon.png");
	# spr.scale = Vector2(2,2);
	self.add_child(spr)

	# 碰撞形状
	# Shape2D 
	var rect = RectangleShape2D.new();
	rect.set_extents(Vector2(spr.texture.get_width(),spr.texture.get_height()));
	var oid = self.create_shape_owner(self);
	self.shape_owner_add_shape(oid,rect);

	# 链接信号
	# 鼠标重叠
	self.input_pickable = true;
	self.connect("mouse_entered",self,"_on_mouse_enter");
	self.connect("mouse_exited",self,"_on_mouse_exited");

	# Area重叠
	self.monitoring = true;
	self.monitorable = true;
	self.connect("area_entered",self,"_on_area_enter")
	self.connect("area_exited",self,"_on_area_exited")
	self.connect("area_shape_entered",self,"_on_shape_enter")
	self.connect("area_shape_exited",self,"_on_shape_exit")

	# Body重叠
	self.connect("body_entered",self,"_on_body_enter")
	self.connect("body_exited",self,"_on_body_exit")
	self.connect("body_shape_entered",self,"_on_body_shape_enter")
	self.connect("body_shape_exited",self,"_on_body_shape_exit")

func _on_mouse_enter():
	print("_on_mouse_enter");

func _on_mouse_exited():
	print("_on_mouse_exited");

func _on_area_enter(area):
	print("_on_area_enter");

func _on_area_exited(area):
	print("_on_area_exited");

func _on_shape_enter(rid,area,i1,i2):
	print("_on_shape_enter");
	
func _on_shape_exit(rid,area,i1,i2):
	print("_on_shape_exit");

func _on_body_enter(body):
	print("_on_body_enter",body);

func _on_body_exit(body):
	print("_on_body_exit",body);

func _on_body_shape_enter(bid,body,i1,i2):
	print("_on_body_shape_enter");
	
func _on_body_shape_exit(bid,body,i1,i2):
	print("_on_body_shape_exit");


func _input(event):
	if(isMove):
		if(event is InputEventKey and event.is_pressed()):
			onKey(event);
		elif(event is InputEventMouseButton and event.is_pressed()):
			onMouse(event);
	
	

func onKey(event:InputEvent):
	var keyCode = event.scancode;

	if(keyCode == KEY_A):
		x = x - xv
	elif(keyCode == KEY_D):
		x = x + xv
	elif(keyCode == KEY_W):
		y = y - yv
	elif(keyCode == KEY_S):
		y = y + yv
	self.position = Vector2(x,y);

func onMouse(mouseEvt):
		if(mouseEvt.button_index == BUTTON_LEFT):
			x = mouseEvt.position.x;
			y = mouseEvt.position.y;

			self.position = mouseEvt.position;
